﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Collections.Specialized
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports Microsoft.Security.Application
Imports Ckeditor_Util

Namespace Controls
    Public Class Ckeditor_Controls
        Inherits HtmlTextArea
        'http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html

        ''' <summary>
        ''' The base href URL used to resolve relative and absolute URLs in the editor content.
        ''' </summary>
        Public Property BaseHref() As String
            Get
                Return If(TryCast(ViewState("BaseHref"), String), "")
            End Get
            Set(ByVal value As String)
                ViewState("BaseHref") = value
            End Set
        End Property

        ''' <summary>
        ''' The height of editing area( content ), in relative or absolute.
        ''' </summary>
        Public Property Height() As Unit
            Get
                Return Parse(Of Unit)(ViewState("Height"))
            End Get
            Set(ByVal value As Unit)
                ViewState("Height") = value
            End Set
        End Property

        ''' <summary>
        ''' The width of editing area( content ), in relative or absolute.
        ''' </summary>
        Public Property Width() As Unit
            Get
                Return Parse(Of Unit)(ViewState("Width"))
            End Get
            Set(ByVal value As Unit)
                ViewState("Width") = value
            End Set
        End Property

        ''' <summary>
        ''' The toolbox (alias toolbar) definition.
        ''' </summary>
        Public Property Toolbar() As String
            Get
                Return If(TryCast(ViewState("Toolbar"), String), "")
            End Get
            Set(ByVal value As String)
                ViewState("Toolbar") = value
            End Set
        End Property

        ''' <summary>
        ''' The CSS file(s) to be used to apply style to the contents. Specify multiple like "['content1.css','content2.css']", note ~ will not be resolved when specifying multiple.
        ''' </summary>
        Public Property ContentsCss() As String
            Get
                Return If(TryCast(ViewState("ContentsCss"), String), "")
            End Get
            Set(ByVal value As String)
                ViewState("ContentsCss") = value
            End Set
        End Property

        Protected Overrides Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As NameValueCollection) As Boolean
            Dim res As Boolean = MyBase.LoadPostData(postDataKey, postCollection)
            Value = AntiXss.GetSafeHtmlFragment(Value.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&"))
            Return res
        End Function

        Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
            Dim settings As New List(Of String)()
            settings.Add("baseHref: '" & ResolveUrl(BaseHref) & "'")
            If Height <> Unit.Empty Then
                settings.Add("height: '" & Convert.ToString(Height) & "'")
            End If
            If Width <> Unit.Empty Then
                settings.Add("width: '" & Convert.ToString(Width) & "'")
            End If
            If Toolbar <> "" Then
                settings.Add("toolbar: '" & Toolbar & "'")
            End If
            If ContentsCss <> "" Then
                settings.Add("contentsCss: '" & (If(ContentsCss.StartsWith("~"), ResolveUrl(ContentsCss), ContentsCss)) & "'")
            End If

            ScriptManager.RegisterClientScriptInclude(Me, Me.[GetType](), "ckeditorScript", ResolveUrl(BaseHref & (If(BaseHref.EndsWith("/"), "", "/")) & "ckeditor.js"))
            ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "ckeditorLoad", "CKEDITOR.replace('" & ClientID & "', { " & [String].Join(", ", settings.ToArray()) & " });", True)

            MyBase.OnPreRender(e)
        End Sub
    End Class
End Namespace
